package com.hnqc.hsl;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Socket;

public class Servertwo {
    private DefaultListModel dt;
    private JTextArea jta;
    private JTextField jtf_message=new JTextField();
    private JTextField jtf_port=new JTextField("4567");
    private JTextField jtf_max=new JTextField("30");
    private JButton jbt1_start=new JButton("登录");
    private JButton jbt2_stop=new JButton("停止");

    //UI设置方法
    public void createUI() {
        JFrame jf = new JFrame("客户端");
        jf.setBounds(200, 200, 500, 300);
        JPanel northPane = new JPanel();
        northPane.setLayout(new GridLayout(1, 6));
        northPane.add(new JLabel("人数上限"));
        northPane.add(jtf_max);
        northPane.add(new JLabel("用户名"));
        northPane.add(jtf_port);
        northPane.add(jbt1_start);
        jbt2_stop.setEnabled(false);
        northPane.add(jbt2_stop);
        northPane.setBorder(new TitledBorder("配置信息"));
        dt=new DefaultListModel();
        final JList jl=new JList();
        JScrollPane leftpane=new JScrollPane(jl);
        leftpane.setBorder(new TitledBorder("在线用户"));
        jta=new JTextArea();
        jta.setEditable(false);
        JScrollPane rightpane =new JScrollPane(jta);
        rightpane.setBorder(new TitledBorder("消息显示区"));
        JSplitPane centerPane=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,leftpane,rightpane);
        centerPane.setDividerSize(20);
        centerPane.setDividerLocation(100);
        JPanel southPanel=new JPanel(new BorderLayout());
        southPanel.setBorder(new TitledBorder("公告"));
        southPanel.add(jtf_message,BorderLayout.CENTER);
        //布局设置
        jf.setLayout(new BorderLayout());
        jf.add(northPane,BorderLayout.NORTH);
        jf.add(centerPane,BorderLayout.CENTER);
        jf.add(southPanel,BorderLayout.SOUTH);
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        client();
    }

    //按键监听事件
    private void client() {
        jtf_message.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                super.keyPressed(e);
                if(e.getKeyChar()==KeyEvent.VK_ENTER){
                    System.out.println(jtf_message.getText());
                    jtf_message.setText("");
                }
            }
        });
        //获取新端口
        jbt1_start.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String port=jtf_port.getText().trim();//
                int port_new=Integer.parseInt(port);
                try {
                    Socket socket=new Socket("127.0.0.1",port_new);
                    //连接服务器读取消息
                    InputStream in=socket.getInputStream();
                    BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(in));
                    String str=bufferedReader.readLine();
                    jta.append(str);

                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        });
    }

    public static void main(String[] args) {
        new Servertwo().createUI();
    }
}
